package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.amap.api.location.AMapLocation;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.SimpleWebViewParams;
import com.isunland.managesystem.utils.LocationUtil;
import com.isunland.managesystem.utils.MenuUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.WebViewUtil;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/7/10 14:42
 */
public class SimpleWebViewFragment extends BaseFragment {
  private static final int MENU_ITEM_REFRESH = 1;
  @BindView(R.id.wv_map) WebView mWv;
  private SimpleWebViewParams mParams;

  @Override public void initData() {
    super.initData();
    mParams = mBaseParams instanceof SimpleWebViewParams ? (SimpleWebViewParams) mBaseParams
        : new SimpleWebViewParams();
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_task_path, container, false);
    ButterKnife.bind(this, v);
    return v;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(mParams.getTitle());
    WebViewUtil.init(mActivity, mWv, mParams.isShowLoadingDialog());
    if (mParams.isLoadLocationUrl()) refreshLocation();
    if (mParams.isLoadUrl()) mWv.loadUrl(mParams.getUrl());
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if (mParams.isRefreshButton()) {
      MenuUtil.addSubMenu(menu, MENU_ITEM_REFRESH, R.string.refresh)
          .setIcon(R.drawable.refresh)
          .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item == null) return false;
    if (item.getItemId() == MENU_ITEM_REFRESH) {
      if (mParams.isLoadLocationUrl()) refreshLocation();
      if (mParams.isLoadUrl()) mWv.loadUrl(mParams.getUrl());
    }
    return super.onOptionsItemSelected(item);
  }

  private void refreshLocation() {
    LocationUtil.newInstance(mActivity)
        .setSimpleLocationListener(new LocationUtil.SimpleCustomLocationListener() {
          @Override public void onLocationValid(AMapLocation location) {
            mWv.loadUrl(getLocationUrl(location));
          }

          @Override public void onLocationError() {
            showDialog(MessageDialog.newInstence("无法获取位置,请检查定位权限!"), 0, "location");
            mWv.loadUrl(getLocationUrl(null));
          }
        })
        .startLocation();
  }

  private String getLocationUrl(AMapLocation location) {
    //即使经纬度为空,也加载地图,所以也要有地址.
    String latStr = location == null ? "" : String.valueOf(location.getLatitude());
    String lonStr = location == null ? "" : String.valueOf(location.getLongitude());
    return ApiConst.getAbsoluteUrl(
        MyStringUtil.connect(mParams.getUrl(), "?latitude=", latStr, "&longitude=", lonStr));
  }

  //@Override public void onDestroy() {
  //  LocationUtil.newInstance(mActivity).destroy();
  //  super.onDestroy();
  //}
}
